package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.EduAppBean;
import com.linkage.educloud.ah.widget.Flowlayout;
import com.zzhou.common.library.util.StringUtil;
import com.zzhou.common.library.util.ToastShow;

import java.util.Arrays;
import java.util.List;

/**
 * Created by Mr'Tang on 2018/12/19.
 */

public class EduAppAdapter extends XRecyclerView.Adapter<EduAppAdapter.ViewHolder> {
    private List<EduAppBean> mlist;
    private Context mcontext;

    public EduAppAdapter(Context context, List<EduAppBean> datas) {
        this.mcontext = context;
        this.mlist = datas;
    }

    public void addAll(List<EduAppBean> data) {
        mlist = data;
        notifyDataSetChanged();
    }

    public void addAllForSearch(List<EduAppBean> data, boolean append) {
        if (mlist != null) {
            if (!append) {
                mlist.clear();
            }
            mlist.addAll(data);
        } else {
            mlist = data;
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_edu_app, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        EduAppBean info = mlist.get(position);
        holder.item_edu_app_name.setText(info.getAppname());
        if (info.getIsorder() == 0) {
            holder.item_edu_app_onorder.setVisibility(View.GONE);
        } else {
            holder.item_edu_app_onorder.setVisibility(View.VISIBLE);
        }
        if (info.getIstrytouse() == 0) {
            holder.item_edu_app_experience.setVisibility(View.GONE);
        } else {
            holder.item_edu_app_experience.setVisibility(View.VISIBLE);
        }
        if (info.getIsrenew() == 1) {
            holder.item_edu_app_renew.setVisibility(View.VISIBLE);
        } else {
            holder.item_edu_app_renew.setVisibility(View.GONE);
        }

        //不是免费的情况下  价格还返回0时  不显示免费
        if (info.getIsfree() == 0) {
            holder.item_edu_app_price.setVisibility(View.VISIBLE);
            holder.item_edu_app_price.setText("免费");
            holder.item_edu_app_price.setTextColor(Color.parseColor("#00cb71"));
            holder.item_edu_app_price.setBackgroundResource(R.drawable.ic_eduapp_price_bg_green);
        } else {
            if (info.getPrice() == 0) {
                holder.item_edu_app_price.setVisibility(View.GONE);
            } else {
                holder.item_edu_app_price.setVisibility(View.VISIBLE);
                holder.item_edu_app_price.setText("￥" + info.getPrice() + "元/" + info.getUnit());
                holder.item_edu_app_price.setTextColor(mcontext.getResources().getColor(R.color.red));
                holder.item_edu_app_price.setBackgroundResource(R.drawable.ic_eduapp_price_bg_red);
            }
        }
        if (info.getSupplier().length() == 0 || info.getSupplier().isEmpty() || "null".equals(info.getSupplier())) {
            holder.item_edu_app_provider.setText("服务器提供商：");
        } else {
            holder.item_edu_app_provider.setText("服务器提供商：" + info.getSupplier());
        }

        holder.item_edu_app_usenum.setText("使用人数：" + info.getUsernum());
        if (!info.getApply().isEmpty()) {
            String[] strs = info.getApply().split(",");
            if (strs != null && strs.length > 0) {
                List<String> myApply = Arrays.asList(strs);
                holder.item_edu_app_flow_apply.setVisibility(View.VISIBLE);
                holder.item_edu_app_flow_apply.removeAllViews();
                for (int a = 0; a < myApply.size(); a++) {
                    TextView myText = new TextView(mcontext);
                    myText.setTextSize(12);
                    myText.setTextColor(Color.parseColor("#14abfd"));
                    myText.setText(StringUtil.cuteString(myApply.get(a), 5));
                    myText.setBackgroundResource(R.drawable.flow_textview_bg);
                    ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    lp.setMargins(10, 10, 10, 10);
                    holder.item_edu_app_flow_apply.addView(myText, lp);
                }
            } else {
                holder.item_edu_app_flow_apply.setVisibility(View.GONE);
            }
        }

        if (!info.getSign().isEmpty()) {
            String[] strs1 = info.getSign().split(",");
            if (strs1 != null && strs1.length > 0) {
                List<String> myLabel = Arrays.asList(strs1);
                holder.item_edu_app_flow_label.setVisibility(View.VISIBLE);
                holder.item_edu_app_flow_label.removeAllViews();
                for (int a = 0; a < myLabel.size(); a++) {
                    TextView myText = new TextView(mcontext);
                    myText.setTextSize(12);
                    myText.setTextColor(Color.parseColor("#14abfd"));
                    myText.setText(StringUtil.cuteString(myLabel.get(a), 5));
                    myText.setBackgroundResource(R.drawable.flow_textview_bg);
                    ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    lp.setMargins(10, 10, 10, 10);
                    holder.item_edu_app_flow_label.addView(myText, lp);
                }
            } else {
                holder.item_edu_app_flow_label.setVisibility(View.GONE);
            }
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastShow.show(mcontext, "待添加");
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView item_edu_app_name;
        ImageView item_edu_app_onorder;
        ImageView item_edu_app_renew;
        ImageView item_edu_app_experience;
        TextView item_edu_app_price;
        TextView item_edu_app_provider;
        Flowlayout item_edu_app_flow_apply;
        Flowlayout item_edu_app_flow_label;
        TextView item_edu_app_usenum;

        public ViewHolder(View convertView) {
            super(convertView);
            item_edu_app_name = convertView.findViewById(R.id.item_edu_app_name);
            item_edu_app_onorder = convertView.findViewById(R.id.item_edu_app_onorder);
            item_edu_app_renew = convertView.findViewById(R.id.item_edu_app_renew);
            item_edu_app_experience = convertView.findViewById(R.id.item_edu_app_experience);
            item_edu_app_price = convertView.findViewById(R.id.item_edu_app_price);
            item_edu_app_provider = convertView.findViewById(R.id.item_edu_app_provider);
            item_edu_app_flow_apply = convertView.findViewById(R.id.item_edu_app_flow_apply);
            item_edu_app_flow_label = convertView.findViewById(R.id.item_edu_app_flow_label);
            item_edu_app_usenum = convertView.findViewById(R.id.item_edu_app_usenum);
        }
    }
}
